import sys, pygame
import os
pygame.init()

size = width, height = 640, 480
speed = [0, 1]
black = 0, 0, 0

screen = pygame.display.set_mode(size)

ball = pygame.image.load("images" + os.sep + "tank.png")
ballrect = ball.get_rect()

#ns,   ps
statetable = [(( 0, 1), ( 1, 0)),
              (( 1, 0), ( 0,-1)),
              (( 0,-1), (-1, 0)),
              ((-1, 0), (0, 1))]

speed = (0, 1)

while 1:
	for event in pygame.event.get():
		if event.type == pygame.QUIT: sys.exit()

	ballrect = ballrect.move((speed[0], speed[1]))
				
	if (ballrect.bottom > height) or (ballrect.right > width) or (ballrect.top < 0) or (ballrect.left < 0): 
		for i in range(0, 4):
			if (speed == statetable[i][0]):
				speed = statetable[i][1]

				if (ballrect.bottom > height):
					ballrect.bottom = height
				if (ballrect.top < 0):
					ballrect.top = 0
				if (ballrect.right > width):
					ballrect.right = width
				if (ballrect.left < 0):
					ballrect.left = 0

				ball = pygame.transform.rotate(ball, 90)

				print "right=%i, bottom=%i, left=%i, top=%i speed(%i,%i)" % (ballrect.right, ballrect.bottom, ballrect.left, ballrect.top,speed[0],speed[1])
				break
				
	

	
	screen.fill(black)
	screen.blit(ball, ballrect)
	pygame.display.flip()

